import { ActionTree } from "vuex";
import { online, asyncadminselectactivity, asyncselectarticle, asyncselectquestion, asyncadminselectquestionbank, asyncselectuser, Iobj, asyncselectlabel, asyncgetusercount, asyncgetquestioncount, asyncgetactivitycount, asyncgetarticlecount, asyncgetquestionbankcount, asyncgetlabelcount, asyncselectteams, asyncadminselectquestion, asyncselectpackagekey, asyncselectspecialjudge, asyncselectteam } from '../../api'
import * as types from './mutation-types'
import Study from '../../mysql_interface/studycodemodel';
import { Istate } from "../../interface";
import { ElMessage } from "element-plus";

const actions: ActionTree<Istate, Istate> = {
  async actiononline({ commit }) {
    try {
      const data = await online()
      if (!['manage', 'admin'].includes(String(data.type))) {
        ElMessage.error('权限不足')
        location.href = '/'
      }
      commit(types.SET_U, data)
      return true
    } catch (e: any) {
      return false
    }
  },
  async actionusers({ commit }, body?: Iobj<Study.Iuser>) {
    try {
      const data = await asyncselectuser(body)
      commit(types.SET_USERS, data)
    } catch (e: any) {
      commit(types.SET_USERS, [])
    }
  },
  async actionactivity({ commit }, body: Iobj<Study.Iactivity> = {}) {
    try {

      const data = await asyncadminselectactivity(body)
      commit(types.SET_ACTIVITY, data)
    } catch (e: any) {
      commit(types.SET_ACTIVITY, [])
    }
  },
  async actionarticle({ commit }, body?: Iobj<Study.Iarticle>) {
    try {
      const data = await asyncselectarticle(body)
      commit(types.SET_ARTICLE, data)
    } catch (e: any) {
      commit(types.SET_ARTICLE, [])
    }
  },
  async actionquestion({ commit }, body: Iobj<Study.Iquestion> = {}) {
    if (!body._sortable) {
      body._sortable = 'ASC'
    }
    try {
      const data = await asyncadminselectquestion(body)
      commit(types.SET_QUESTION, data)
    } catch (e: any) {
      commit(types.SET_QUESTION, [])
    }
  },
  async actionquestionbank({ commit }, body?: Iobj<Study.Iquestionbank>) {
    try {
      const data = await asyncadminselectquestionbank(body)
      commit(types.SET_QUESTIONBANK, data)
    } catch (e: any) {
      commit(types.SET_QUESTIONBANK, [])
    }
  },
  async actionlabel({ commit }, body?: Iobj<Study.ILabel>) {
    try {
      const data = await asyncselectlabel()
      commit(types.SET_LABEL, data)
    } catch (e: any) {
      commit(types.SET_LABEL, [])
    }
  },
  async actionteam({ commit }, body: any = {}) {
    try {
      const data = await asyncselectteam(body)
      commit(types.SET_TEAM, data)
    } catch (e: any) {
      commit(types.SET_TEAM, [])
    }
  },
  async actionpackegekey({ commit }, body: any = {}) {
    try {
      const data = await asyncselectpackagekey(body)
      commit(types.SET_PACKEGEKEY, data)
    } catch (e) {
      commit(types.SET_PACKEGEKEY, [])
      return e
    }
  },
  async actionspecialjudge({ commit }, body: any = {}) {
    try {
      const data = await asyncselectspecialjudge(body)
      commit(types.SET_SPECIALJUDGE, data)
    } catch (e) {
      commit(types.SET_SPECIALJUDGE, [])
      return e
    }
  }
}


export default actions